package com.yakaten.logic;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.seasar.framework.util.ResourceUtil;

import com.yakaten.dto.KakakuPriceTaskDto;
import com.yakaten.exception.SystemException;

public class CopyOfKakakuPriceLogic {

	private static final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final String TASK_FILE_NAME_KEY="system.task.file.kakaku";
	public List<KakakuPriceTaskDto> getAllTaskList() throws SystemException {
		List<KakakuPriceTaskDto> resultList = new ArrayList<KakakuPriceTaskDto>();
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
				.getProperty(TASK_FILE_NAME_KEY);

		InputStreamReader ir = null;
		try {
			ir = new InputStreamReader(new FileInputStream(
					taskFile), "shift_jis");

			BufferedReader br = new BufferedReader(ir);
			String current = "";
			while ((current = br.readLine()) != null) {
				String data[] =current.split(",");
				KakakuPriceTaskDto dto = new KakakuPriceTaskDto();
				dto.setTaskNo(data[0]);
				dto.setKakakuItemId(data[1]);
				dto.setKakakuItemName(data[2]);
				dto.setKakakuItemPrice(Long.parseLong(data[3]));
				dto.setShopItemId(data[4]);
				dto.setShopItemName(data[5]);
				dto.setShopItemPrice(Long.parseLong(data[6]));
				dto.setStartPrice(Long.parseLong(data[7]));
				dto.setMinPrice(Long.parseLong(data[8]));
				dto.setStartTime(sf.parse(data[9]));
				dto.setStatus(Long.parseLong(data[10]));
				dto.setQuantity(data[11]);

				try{
					dto.setKakakuItemSyoryuCalculate(data[12]);
				}catch(Exception e){
					dto.setKakakuItemSyoryuCalculate("0");
				}

				resultList.add(dto);
			}
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
//			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		} catch (ParseException e) {
			throw new SystemException(e);
		}finally{
			if(ir!=null){
				try {
					ir.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return resultList;
	}

	public boolean isExistTask(String kakakuItemId,String shopItemId) throws SystemException{
		boolean isExit = false;
		 List<KakakuPriceTaskDto> allTask =  getAllTaskList();
		 for(KakakuPriceTaskDto current : allTask){
			 if(current.getShopItemId().equals(shopItemId) || current.getKakakuItemId().equals(kakakuItemId)){
				 isExit = true;
				 break;
			 }
		 }

		return isExit;
	}

	public String getNewTaskNo() throws SystemException{
		String newTaskNo="1";
		List<KakakuPriceTaskDto> list = this.getAllTaskList();
		if(list.size()!=0){
			KakakuPriceTaskDto dto = list.get(list.size()-1);
			newTaskNo =""+(Long.parseLong(dto.getTaskNo())+1);
		}
		return newTaskNo;
	}
	public synchronized boolean addTask(KakakuPriceTaskDto dto) throws SystemException{
		boolean result =false;
		OutputStreamWriter or =null;
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
		.getProperty(TASK_FILE_NAME_KEY);
		try {
			or = new OutputStreamWriter(new FileOutputStream(
					taskFile,true), "shift_jis");
			StringBuffer data = new StringBuffer();
			data.append(dto.getTaskNo()+",");
			data.append(dto.getKakakuItemId()+",");
			data.append(dto.getKakakuItemName()+",");
			data.append(dto.getKakakuItemPrice()+",");
			data.append(dto.getShopItemId()+",");
			data.append(dto.getShopItemName()+",");
			data.append(dto.getShopItemPrice()+",");
			data.append(dto.getStartPrice()+",");
			data.append(dto.getMinPrice()+",");
			data.append(sf.format(dto.getStartTime())+",");
			data.append(dto.getStatus()+",");
			data.append(dto.getQuantity()+",");
			data.append(dto.getKakakuItemSyoryuCalculate()+"\r\n");
			or.write(data.toString());
			or.flush();
			result = true;
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		}finally{
			if(or!=null){
				try {
					or.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public synchronized boolean updateTask(List<KakakuPriceTaskDto> dataList) throws SystemException{
		boolean result =false;
		List<KakakuPriceTaskDto> list = this.getAllTaskList();
		Iterator<KakakuPriceTaskDto> it = list.iterator();
		while(it.hasNext()){
			KakakuPriceTaskDto current =it.next();
			Iterator<KakakuPriceTaskDto> dataIt = dataList.iterator();{
				while(dataIt.hasNext()){
					KakakuPriceTaskDto updateDto = dataIt.next();
					if(current.getTaskNo().equals(updateDto.getTaskNo())){
						current.setKakakuItemId(updateDto.getKakakuItemId());
						current.setKakakuItemName(updateDto.getKakakuItemName());
						current.setKakakuItemPrice(updateDto.getKakakuItemPrice());
						current.setShopItemId(updateDto.getShopItemId());
						current.setShopItemName(updateDto.getShopItemName());
						current.setShopItemPrice(updateDto.getShopItemPrice());
						current.setStartPrice(updateDto.getStartPrice());
						current.setMinPrice(updateDto.getMinPrice());
						current.setStartTime(updateDto.getStartTime());
						current.setStatus(updateDto.getStatus());
						current.setQuantity(updateDto.getQuantity());
						current.setKakakuItemSyoryuCalculate(updateDto.getKakakuItemSyoryuCalculate());
					}
				}
			}

		}

		OutputStreamWriter or =null;
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
		.getProperty(TASK_FILE_NAME_KEY);
		try {
			or = new OutputStreamWriter(new FileOutputStream(
					taskFile), "shift_jis");

			Iterator<KakakuPriceTaskDto> nit = list.iterator();
			while(nit.hasNext()){
				KakakuPriceTaskDto dto =nit.next();
				StringBuffer data = new StringBuffer();
				data.append(dto.getTaskNo()+",");
				data.append(dto.getKakakuItemId()+",");
				data.append(dto.getKakakuItemName()+",");
				data.append(dto.getKakakuItemPrice()+",");
				data.append(dto.getShopItemId()+",");
				data.append(dto.getShopItemName()+",");
				data.append(dto.getShopItemPrice()+",");
				data.append(dto.getStartPrice()+",");
				data.append(dto.getMinPrice()+",");
				data.append(sf.format(dto.getStartTime())+",");
				data.append(dto.getStatus()+",");
				data.append(dto.getQuantity()+",");
				data.append(dto.getKakakuItemSyoryuCalculate()+"\r\n");
				or.write(data.toString());
			}
			or.flush();
			result=true;
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
//			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		}finally{
			if(or!=null){
				try {
					or.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public synchronized boolean updateTask(KakakuPriceTaskDto updateDto) throws SystemException{
		boolean result =false;
		List<KakakuPriceTaskDto> list = this.getAllTaskList();
		Iterator<KakakuPriceTaskDto> it = list.iterator();
		while(it.hasNext()){
			KakakuPriceTaskDto current =it.next();
			if(current.getTaskNo().equals(updateDto.getTaskNo())){
				current.setKakakuItemId(updateDto.getKakakuItemId());
				current.setKakakuItemName(updateDto.getKakakuItemName());
				current.setKakakuItemPrice(updateDto.getKakakuItemPrice());
				current.setShopItemId(updateDto.getShopItemId());
				current.setShopItemName(updateDto.getShopItemName());
				current.setShopItemPrice(updateDto.getShopItemPrice());
				current.setStartPrice(updateDto.getStartPrice());
				current.setMinPrice(updateDto.getMinPrice());
				current.setStartTime(updateDto.getStartTime());
				current.setStatus(updateDto.getStatus());
				current.setQuantity(updateDto.getQuantity());
				current.setKakakuItemSyoryuCalculate(updateDto.getKakakuItemSyoryuCalculate());
			}
		}

		OutputStreamWriter or =null;
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
		.getProperty(TASK_FILE_NAME_KEY);
		try {
			or = new OutputStreamWriter(new FileOutputStream(
					taskFile), "shift_jis");

			Iterator<KakakuPriceTaskDto> nit = list.iterator();
			while(nit.hasNext()){
				KakakuPriceTaskDto dto =nit.next();
				StringBuffer data = new StringBuffer();
				data.append(dto.getTaskNo()+",");
				data.append(dto.getKakakuItemId()+",");
				data.append(dto.getKakakuItemName()+",");
				data.append(dto.getKakakuItemPrice()+",");
				data.append(dto.getShopItemId()+",");
				data.append(dto.getShopItemName()+",");
				data.append(dto.getShopItemPrice()+",");
				data.append(dto.getStartPrice()+",");
				data.append(dto.getMinPrice()+",");
				data.append(sf.format(dto.getStartTime())+",");
				data.append(dto.getStatus()+",");
				data.append(dto.getQuantity()+",");
				data.append(dto.getKakakuItemSyoryuCalculate()+"\r\n");
				or.write(data.toString());
			}
			or.flush();
			result=true;
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
//			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		}finally{
			if(or!=null){
				try {
					or.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public synchronized boolean deleteTask(String taskNo) throws SystemException{
		boolean result =false;
		List<KakakuPriceTaskDto> list = this.getAllTaskList();
		List<KakakuPriceTaskDto> workList = new ArrayList<KakakuPriceTaskDto>();
		for(int i=0;i<list.size();i++){
			KakakuPriceTaskDto current = list.get(i);
			if(!current.getTaskNo().equals(taskNo)){
				workList.add(current);
			}
		}

		OutputStreamWriter or =null;
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
		.getProperty(TASK_FILE_NAME_KEY);
		try {
			or = new OutputStreamWriter(new FileOutputStream(
					taskFile), "shift_jis");

			Iterator<KakakuPriceTaskDto> nit = workList.iterator();
			while(nit.hasNext()){
				KakakuPriceTaskDto dto =nit.next();
				StringBuffer data = new StringBuffer();
				data.append(dto.getTaskNo()+",");
				data.append(dto.getKakakuItemId()+",");
				data.append(dto.getKakakuItemName()+",");
				data.append(dto.getKakakuItemPrice()+",");
				data.append(dto.getShopItemId()+",");
				data.append(dto.getShopItemName()+",");
				data.append(dto.getShopItemPrice()+",");
				data.append(dto.getStartPrice()+",");
				data.append(dto.getMinPrice()+",");
				data.append(sf.format(dto.getStartTime())+",");
				data.append(dto.getStatus()+",");
				data.append(dto.getQuantity()+",");
				data.append(dto.getKakakuItemSyoryuCalculate()+"\r\n");
				or.write(data.toString());

			}
			or.flush();
			result=true;
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
//			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		}finally{
			if(or!=null){
				try {
					or.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	public synchronized boolean updateTaskStatus(String taskNo,long status) throws SystemException{
		boolean result =false;
		List<KakakuPriceTaskDto> list = this.getAllTaskList();
		Iterator<KakakuPriceTaskDto> it = list.iterator();
		while(it.hasNext()){
			KakakuPriceTaskDto current =it.next();
			if(current.getTaskNo().equals(taskNo)){
				current.setStatus(status);
			}
		}

		OutputStreamWriter or =null;
		String taskFile = ResourceUtil.getProperties("yakaten.properties")
		.getProperty(TASK_FILE_NAME_KEY);
		try {
			or = new OutputStreamWriter(new FileOutputStream(
					taskFile), "shift_jis");

			Iterator<KakakuPriceTaskDto> nit = list.iterator();
			while(nit.hasNext()){
				KakakuPriceTaskDto dto =nit.next();
				StringBuffer data = new StringBuffer();
				data.append(dto.getTaskNo()+",");
				data.append(dto.getKakakuItemId()+",");
				data.append(dto.getKakakuItemName()+",");
				data.append(dto.getKakakuItemPrice()+",");
				data.append(dto.getShopItemId()+",");
				data.append(dto.getShopItemName()+",");
				data.append(dto.getShopItemPrice()+",");
				data.append(dto.getStartPrice()+",");
				data.append(dto.getMinPrice()+",");
				data.append(sf.format(dto.getStartTime())+",");
				data.append(dto.getStatus()+",");
				data.append(dto.getQuantity()+",");
				data.append(dto.getKakakuItemSyoryuCalculate()+"\r\n");
				or.write(data.toString());

			}
			or.flush();
			result=true;
		} catch (UnsupportedEncodingException e) {
			throw new SystemException(e);
		} catch (FileNotFoundException e) {
//			throw new SystemException(e);
		} catch (IOException e) {
			throw new SystemException(e);
		}finally{
			if(or!=null){
				try {
					or.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return result;
	}


	public static void main(String [] args){
		try {

			CopyOfKakakuPriceLogic logic =new CopyOfKakakuPriceLogic();

			KakakuPriceTaskDto dto = new KakakuPriceTaskDto();
			dto.setTaskNo("1");
			dto.setKakakuItemId("K0000305715");
			dto.setKakakuItemName("MacBook Pro 2400/15.4 MD322J/A");
			dto.setKakakuItemPrice(119620);
			dto.setShopItemId("005006000028");
			dto.setShopItemName("パナソニック Let's note SX1 CF-SX1GEBDR [ブラック]");
			dto.setShopItemPrice(119621);
			dto.setStartPrice(999999);
			dto.setMinPrice(1);
			dto.setStartTime(new Date());
			dto.setStatus(1);
			dto.setQuantity("11");
			logic.addTask(dto);
			System.out.println(logic.getNewTaskNo());
//			logic.getAllTaskList();
		} catch (SystemException e) {
			e.printStackTrace();
		}
	}
}
